package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author xiaycbstart
 * @create 2019 12 22 22:57
 */
public interface SetmealDao {
    @Insert("insert into t_setmeal values(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @Options(useGeneratedKeys = true,keyColumn = "id")
    void addSetmeal(Setmeal setmeal);
    @Insert("insert into t_setmeal_checkgroup values(#{setmeal_id},#{checkgroup_id})")
    void addRelation(Map<String, Integer> map);
    @Select("<script>" +
            "select * from t_setmeal\n" +
            "<if test=\"value != null and value.length > 0\">\n" +
            "where code like '%' #{value} '%' or name like '%' #{value} '%' or helpCode like '%' #{value} '%' \n" +
            "</if>" +
            "</script>")
    Page<Setmeal> pageQuery(String value);
    @Select("select * from t_setmeal")
    List<Setmeal> findAll();
    @Select("select * from t_setmeal where id = #{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "code",column = "code"),
            @Result(property = "helpCode",column = "helpCode"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "age",column = "age"),
            @Result(property = "price",column = "price"),
            @Result(property = "remark",column = "remark"),
            @Result(property = "attention",column = "attention"),
            @Result(property = "img",column = "img"),
            @Result(property = "checkGroups",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckGroupDao.findBySetmealId"))
    })
    Setmeal findById(Integer id);
    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{id}")
    List<Integer> findCheckGroupIdsBySetmealId(Integer id);
    @Select("select s.name ,count(o.id) as value from t_order o ,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map> findSetmealCount();
}
